package com.frxuexi.park2212.repair

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.SearchView.OnQueryTextListener
import android.widget.TextView
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.base.BaseActivity
import com.frxuexi.lib_base.entity.SearchBean
import com.frxuexi.park2212.R
import com.frxuexi.park2212.databinding.ActivityRepairBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/RepairActivity")
@AndroidEntryPoint
class RepairActivity : BaseActivity<ActivityRepairBinding, RepairViewModel>() {
    val repairAdapter by lazy { RepairAdapter() }
    var flag = false//false:升序，true:降序
    var isClick=false
    override fun initData() {
        vm.getRepir.observe(this) {
            repairAdapter.data.clear()
            repairAdapter.addData(it)
            repairAdapter.notifyDataSetChanged()
        }

        vm.getRepirByNo.observe(this){
            repairAdapter.data.clear()
            repairAdapter.addData(it)
            repairAdapter.notifyDataSetChanged()
        }

        vm.inserSearch.observe(this){
            Log.i("===",it.toString())
            vm.getSearchs()
        }

        vm.deleteSearch.observe(this){
            if(it>0){
                vm.getSearchs()
            }
        }

        vm.getSearchs.observe(this){his->
            binding.flex.removeAllViews()
            his.forEach {bean->
                val view = LayoutInflater.from(this).inflate(R.layout.flex_history, null)
                view.findViewById<TextView>(R.id.flex_title).apply {
                    setText(bean.name)
                    setOnClickListener {
                        binding.repairSv.setText(bean.name)
                        vm.getRepairByNo(bean.name)
                    }
                }

                view.findViewById<ImageView>(R.id.flex_delete).setOnClickListener {
                    vm.deleteSearch(bean)
                }

                binding.flex.addView(view)
            }
        }
    }

    override fun initView() {
        binding.rvRepair.apply {
            adapter = repairAdapter
            layoutManager = LinearLayoutManager(this@RepairActivity)
        }

        val helper =
            ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
                override fun onMove(
                    recyclerView: RecyclerView,
                    viewHolder: RecyclerView.ViewHolder,
                    target: RecyclerView.ViewHolder
                ): Boolean {
                    return false
                }

                override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                    val position = viewHolder.adapterPosition
                    repairAdapter.data.removeAt(position)
                    repairAdapter.notifyItemRemoved(position)
//                    ToastUtils.showLong(repairAdapter.data.get(position).id!!.toString())
                    vm.deleteRepair(repairAdapter.data.get(position).id!!)
                }

            })
        helper.attachToRecyclerView(binding.rvRepair)

        binding.headRepair.headerTitle.setText("维修管理")
        binding.headRepair.headerBack.setOnClickListener {
            finish()
        }

        vm.getSearchs()

        binding.headRepair.headerAdd.setOnClickListener {
            ARouter.getInstance().build("/app/RepairAddActivity").navigation()
        }

        repairAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/RepairDetailActivity").withString("rid",repairAdapter.data.get(position).id.toString())
                .navigation()
        }

        binding.repairOrder.setOnClickListener {
            if (flag) {
                repairAdapter.data.sortByDescending { it.repairTime }
                binding.repairOrderiv.setImageResource(R.drawable.ic_action_down)
            } else {
                repairAdapter.data.sortBy { it.repairTime }
                binding.repairOrderiv.setImageResource(R.drawable.ic_action_asc)
            }
            flag = !flag
            repairAdapter.notifyDataSetChanged()
        }

        binding.btnSearch.setOnClickListener {
            val query = binding.repairSv.text.toString()
            if(isClick){
                vm.getRepairByNo(query!!)
            }else {
                vm.getRepairsByUname(query!!)
            }
            vm.insertSearch(SearchBean(name = query!!))
        }

        binding.repairSv.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if(s.isNullOrEmpty()){
                    vm.getRepairs()
                }
            }

            override fun afterTextChanged(s: Editable?) {
            }

        })

//        binding.repairSv.setOnCloseListener {
//            vm.getRepairs()
//            return@setOnCloseListener true
//        }

        binding.repairFilter.setOnClickListener {
            isClick=true
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getRepairs()
    }

}